namespace EF.Framework
{
    using System;
    using System.Data.Entity;
    using EF.Framework.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

    public partial class MyuContext : DbContext
    {
        public MyuContext()
            : base("name=MyuContext")
        {
        }

        public virtual DbSet<Blogs> Blogs { get; set; }
        public virtual DbSet<Permissions> Permissions { get; set; }
        public virtual DbSet<Posts> Posts { get; set; }
        public virtual DbSet<RolePermissions> RolePermissions { get; set; }
        public virtual DbSet<Roles> Roles { get; set; }
        public virtual DbSet<UserRoles> UserRoles { get; set; }
        public virtual DbSet<Users> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Entity<Permissions>()
                .HasMany(e => e.RolePermissions)
                .WithRequired(e => e.Permissions)
                .HasForeignKey(e => e.PermissionID);

            modelBuilder.Entity<Roles>()
                .HasMany(e => e.RolePermissions)
                .WithRequired(e => e.Roles)
                .HasForeignKey(e => e.RoleID);

            modelBuilder.Entity<Roles>()
                .HasMany(e => e.UserRoles)
                .WithRequired(e => e.Roles)
                .HasForeignKey(e => e.RoleID);

            modelBuilder.Entity<Users>()
                .HasMany(e => e.UserRoles)
                .WithRequired(e => e.Users)
                .HasForeignKey(e => e.UserID);
        }
    }
}
